package com.cwt.admin.manage;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.TbBook;
import com.cwt.service.vo.Login;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【系统监控】
 * @date 2023年09月01日 17:34:55
 */
@Api(tags = "系统监控", value = "MonitorController")
@RequestMapping("/monitor")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("monitor")
)
public class MonitorController extends BaseController {

    /**
     * 在线用户分页查询
     *
     * @param info 分页查询条件
     * @return 在线用户分页数据信息
     */
    @RequestMapping(value = "/activeUser", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult page(@RequestBody PageInfo info) {
        // 返回数据对象
        ArrayList<Login> users = new ArrayList<>();

        // 获取所有登录的 用户 session
        List<String> ids = StpUtil.searchSessionId("", 0, -1, false);

        // 便利获取登录信息
        SaSession session;
        for (int i = (info.getPageNum() - 1) * info.getPageSize(), len = Math.min(info.getPageSize() + i, ids.size()); i < len; i++) {
            String id = ids.get(i);
            session = StpUtil.getSessionBySessionId(id);
            users.add(session.getModel("user-" + id.substring(id.lastIndexOf(":") + 1), Login.class));
        }
        return AjaxResult.success(users);
    }

}
